package com.lkx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkx.common.R;
import com.lkx.dto.DishDto;
import com.lkx.mapper.CategoryMapper;
import com.lkx.mapper.DishMapper;
import com.lkx.pojo.Category;
import com.lkx.pojo.Dish;
import com.lkx.pojo.DishFlavor;
import com.lkx.service.DishFlavorService;
import com.lkx.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @ClassName: com.lkx.service.impl.DishServiceImpl
 * @author: likaixin
 * @create: 2022年11月22日 19:37
 * @desc:
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishMapper dishMapper;

    //新增菜品，同时保存对应的口味数据
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);

        Long id = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors=flavors.stream().map(item -> {
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());
        System.out.println(flavors);
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //先根据id查询dish菜品
        Dish dish = this.getById(id);
        //创建扩展dish的对象
        DishDto dishDto=new DishDto();
        //然后copy，把dish对象的值 copy到dishDto的flavors字段里
        BeanUtils.copyProperties(dish,dishDto);
        //创建条件查询对象
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();

        qw.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(qw);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表的基本信息
        this.updateById(dishDto);
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(lqw);
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors=flavors.stream().map(item -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);

    }

    @Override
    public boolean modifyStatus(Long[] ids, Integer status) {
        UpdateWrapper<Dish> wrapper = new UpdateWrapper<>();
        wrapper.in("id",ids).set("status",status);
        boolean flag = super.update(wrapper);

        return flag;
    }

    @Override
    public List<Dish> copies(Long id) {
        return dishMapper.copies(id);
    }


}
